﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow :MonoBehaviour
{

    public Transform m_playerTransform;

    //设定一个角色能看到的最远值
    public float Ahead;

    //设置一个摄像机要移动到的点
    public Vector3 targetPos;

    //设置一个缓动速度插值
    public float smooth;

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (m_playerTransform == null)
            return;

        targetPos = new Vector3(m_playerTransform.position.x, m_playerTransform.position.y, m_playerTransform.position.z);

        if (m_playerTransform.position.x > 0f)
        {
            targetPos = new Vector3(m_playerTransform.position.x + Ahead, transform.position.y, m_playerTransform.position.z + 0.2f);
        }
        else
        {
            targetPos = new Vector3(m_playerTransform.position.x - Ahead, transform.position.y, m_playerTransform.position.z  + 0.2f);
        }

        transform.position = Vector3.Lerp(transform.position, targetPos, smooth * Time.deltaTime);

        float posX = transform.localPosition.x;
        if (transform.localPosition.x <= 3.89f)
            posX = 3.89f;
        else if (transform.localPosition.x >= 28.9f)
            posX = 28.9f;

        float posZ = transform.localPosition.z;
        if (transform.localPosition.z <= 2f)
            posZ = 2f;
        else if (transform.localPosition.z >= 21f)
            posZ = 21f;

        if (transform.localPosition.z >= 21f)
        {
            transform.localPosition = new Vector3(posX, 9, 21f);
            return;
        }
        if (transform.localPosition.z <= 2f)
        {
            transform.localPosition = new Vector3(posX, 9, 2f);
            return;
        }

        if (transform.localPosition.x <= 3.89f)
        {
            transform.localPosition = new Vector3(3.89f, 9, posZ);
            return;
        }
        if (transform.localPosition.x >= 28.9f)//右上
        {
            transform.localPosition = new Vector3(28.9f, 9, posZ);
            return;
        }
    }

    public void SetPlayer(PlayerEntity player)
    {
        //if (m_playerTransform != null)
        //    return;

        m_playerTransform = player.transform;
        transform.position = m_playerTransform.position;

        if (transform.localPosition.z >= 21f)
        {
            transform.localPosition = new Vector3(transform.localPosition.x, 9, 21f);
        }
        else if (transform.localPosition.z <= 2f)
        {
            transform.localPosition = new Vector3(transform.localPosition.x, 9, 2f);
        }
        if (transform.localPosition.x <= 3.89f)
        {
            transform.localPosition = new Vector3(3.89f, 9, transform.localPosition.z);
        }
        else if (transform.localPosition.x >= 28.9f)
        {
            transform.localPosition = new Vector3(28.9f, 9, transform.localPosition.z);
        }
    }
}